﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.Globalization;
using System.IO;
//using Galileo;
using System.Configuration;
using Database;
using ConnectDatabase;
using Galileo;


namespace IBE
{
    public class GDSParser
    {
        public struct GeoData
        {
            public string Code { get; set; }
            public tblGeoAirport Airport { get; set; }
            public tblGeoCity City { get; set; }
            public tblGeoCountry Country { get; set; }
            public tblGeoContinent Continent { get; set; }
        };

        public string GenerateXmlRequest(tblFlightSearchArchive searchArchive, bool isOneway)
        {
            string request = "";
            int adultNum = searchArchive.Adult.Value;
            int childNum = searchArchive.Children.Value;
            int infantNum = searchArchive.Infant.Value;
            int totalSeat = adultNum + childNum;

            if (isOneway)
            {
                #region Declare paramerters
                //AirAvailMods
                GenAvail genAvail = new GenAvail();

                //SuperBBMods
                PassengerType passengerType = new PassengerType();
                Optimize optimize1 = new Optimize();
                Optimize optimize2 = new Optimize();
                GenQuoteInfo genQuoteInfo = new GenQuoteInfo();
                PFInfo pFInfo = new PFInfo();
                #endregion

                #region Input values
                //GenAvail
                genAvail.NumSeats = totalSeat.ToString(); //Số ghế ~ số khách người lớn, trẻ em
                genAvail.Class = ""; //Hạng đặt chỗ
                genAvail.StartDt = searchArchive.DepartureDate.Value.ToString("yyyyMMdd"); //Ngày khởi hành
                genAvail.StartPt = searchArchive.DepartureAirportCode; //Điểm khởi hành
                genAvail.EndPt = searchArchive.DestinationAirportCode; //Điểm đến
                genAvail.StartTm = ""; //Bỏ trống
                genAvail.TmWndInd = "";
                genAvail.StartTmWnd = "";
                genAvail.EndTmWnd = "";
                genAvail.JrnyTm = "";
                genAvail.FltTypeInd = "E";
                genAvail.FltTypePref = "";
                genAvail.StartPtInd = "B";
                genAvail.EndPtInd = "B";
                genAvail.IgnoreTSPref = "N";

                //PassengerType
                int countPax = 1;
                for (int i = 1; i <= adultNum; i++)
                {
                    PassengerType.Psgr structPsgrAdult = new PassengerType.Psgr();
                    structPsgrAdult.LNameNum = i.ToString();
                    structPsgrAdult.PsgrNum = i.ToString();
                    structPsgrAdult.AbsNameNum = i.ToString();
                    structPsgrAdult.PTC = "";
                    structPsgrAdult.TIC = "";
                    passengerType.PsgrAry.Add(structPsgrAdult);
                    countPax++;
                }

                for (int i = countPax; i <= adultNum + childNum; i++)
                {
                    PassengerType.Psgr structPsgrChild = new PassengerType.Psgr();
                    structPsgrChild.LNameNum = i.ToString();
                    structPsgrChild.PsgrNum = i.ToString();
                    structPsgrChild.AbsNameNum = i.ToString();
                    structPsgrChild.PTC = "CNN";
                    structPsgrChild.TIC = "";
                    structPsgrChild.Age = "05";
                    passengerType.PsgrAry.Add(structPsgrChild);
                    countPax++;
                }

                for (int i = countPax; i <= adultNum + childNum + infantNum; i++)
                {
                    PassengerType.Psgr structPsgrInfant = new PassengerType.Psgr();
                    structPsgrInfant.LNameNum = i.ToString();
                    structPsgrInfant.PsgrNum = i.ToString();
                    structPsgrInfant.AbsNameNum = i.ToString();
                    structPsgrInfant.PTC = "INF";
                    structPsgrInfant.TIC = "";
                    passengerType.PsgrAry.Add(structPsgrInfant);
                }

                //optimize1
                optimize1.RecType = "1001";
                optimize1.KlrID.Add("AAFI");

                //Optimize2
                optimize2.RecType = "1425";
                optimize2.KlrID.Add("EROR");
                optimize2.KlrID.Add("GFGQ");
                optimize2.KlrID.Add("GFXI");
                optimize2.KlrID.Add("GFPI");
                optimize2.KlrID.Add("GFRI");
                optimize2.KlrID.Add("GFJG");
                optimize2.KlrID.Add("GFMM");

                //genQuoteInfo
                genQuoteInfo.RulesProcess = "Y";
                genQuoteInfo.AltCurrency = ConfigurationManager.AppSettings["AltCurrency"].ToString();
                genQuoteInfo.EquivCurrency = ConfigurationManager.AppSettings["EquivCurrency"].ToString();

                //PFInfo
                pFInfo.ReqAirVPFs = "Y";
                PFInfo.PF structPF = new PFInfo.PF();
                structPF.StartODRange = "00";
                structPF.EndODRange = "00";
                structPF.CRS = "1G";
                structPF.PCC = ConfigurationManager.AppSettings["hcm_pcc"].ToString();
                structPF.AirV = "";
                structPF.Acct = "";
                structPF.PublishedFaresInd = "Y";
                structPF.Type = "A";
                structPF.AcctCodeRestrict = "";
                pFInfo.PFAry.Add(structPF);
                #endregion

                request = "<FareQuoteSuperBB_11>"
                                    + "<AirAvailMods>"
                                        + genAvail.GetXml()
                                    + "</AirAvailMods>"
                                    + "<SuperBBMods>"
                                        + passengerType.GetXml()
                                        + genQuoteInfo.GetXml()
                                        + pFInfo.GetXml()
                                        + optimize1.GetXml()
                                        + optimize2.GetXml()
                                    + "</SuperBBMods>"
                                + "</FareQuoteSuperBB_11>";
            }
            else
            {
                #region Declare paramerters
                //AirAvailMods
                GenAvail genAvailDeparture = new GenAvail();
                GenAvail genAvailReturn = new GenAvail();

                //SuperBBMods
                PassengerType passengerType = new PassengerType();
                Optimize optimize1 = new Optimize();
                Optimize optimize2 = new Optimize();
                GenQuoteInfo genQuoteInfo = new GenQuoteInfo();
                PFInfo pFInfo = new PFInfo();
                #endregion

                #region Input values
                //GenAvailDeparture
                genAvailDeparture.NumSeats = totalSeat.ToString(); //Số ghế ~ số khách người lớn, trẻ em
                genAvailDeparture.Class = ""; //Hạng đặt chỗ
                genAvailDeparture.StartDt = searchArchive.DepartureDate.Value.ToString("yyyyMMdd"); //Ngày khởi hành
                genAvailDeparture.StartPt = searchArchive.DepartureAirportCode; //Điểm khởi hành
                genAvailDeparture.EndPt = searchArchive.DestinationAirportCode; //Điểm đến
                genAvailDeparture.StartTm = ""; //Bỏ trống
                genAvailDeparture.TmWndInd = "";
                genAvailDeparture.StartTmWnd = "";
                genAvailDeparture.EndTmWnd = "";
                genAvailDeparture.JrnyTm = "";
                genAvailDeparture.FltTypeInd = "E";
                genAvailDeparture.FltTypePref = "";
                genAvailDeparture.StartPtInd = "B";
                genAvailDeparture.EndPtInd = "B";
                genAvailDeparture.IgnoreTSPref = "N";

                //GenAvailReturn
                genAvailReturn.NumSeats = totalSeat.ToString(); //Số ghế ~ số khách người lớn, trẻ em
                genAvailReturn.Class = ""; //Hạng đặt chỗ
                genAvailReturn.StartDt = searchArchive.ReturnDate.Value.ToString("yyyyMMdd"); //Ngày về
                genAvailReturn.StartPt = searchArchive.DestinationAirportCode; //Điểm khởi hành (Lúc này lại là điểm điến)
                genAvailReturn.EndPt = searchArchive.DepartureAirportCode; //Điểm đến (Lúc này lại là điểm khởi hành)
                genAvailReturn.StartTm = ""; //Bỏ trống
                genAvailReturn.TmWndInd = "";
                genAvailReturn.StartTmWnd = "";
                genAvailReturn.EndTmWnd = "";
                genAvailReturn.JrnyTm = "";
                genAvailReturn.FltTypeInd = "E";
                genAvailReturn.FltTypePref = "";
                genAvailReturn.StartPtInd = "B";
                genAvailReturn.EndPtInd = "B";
                genAvailReturn.IgnoreTSPref = "N";

                //PassengerType
                int countPax = 1;
                for (int i = 1; i <= adultNum; i++)
                {
                    PassengerType.Psgr structPsgrAdult = new PassengerType.Psgr();
                    structPsgrAdult.LNameNum = i.ToString();
                    structPsgrAdult.PsgrNum = i.ToString();
                    structPsgrAdult.AbsNameNum = i.ToString();
                    structPsgrAdult.PTC = "";
                    structPsgrAdult.TIC = "";
                    passengerType.PsgrAry.Add(structPsgrAdult);
                    countPax++;
                }

                for (int i = countPax; i <= adultNum + childNum; i++)
                {
                    PassengerType.Psgr structPsgrChild = new PassengerType.Psgr();
                    structPsgrChild.LNameNum = i.ToString();
                    structPsgrChild.PsgrNum = i.ToString();
                    structPsgrChild.AbsNameNum = i.ToString();
                    structPsgrChild.PTC = "CNN";
                    structPsgrChild.TIC = "";
                    structPsgrChild.Age = "05";
                    passengerType.PsgrAry.Add(structPsgrChild);
                    countPax++;
                }

                for (int i = countPax; i <= adultNum + childNum + infantNum; i++)
                {
                    PassengerType.Psgr structPsgrInfant = new PassengerType.Psgr();
                    structPsgrInfant.LNameNum = i.ToString();
                    structPsgrInfant.PsgrNum = i.ToString();
                    structPsgrInfant.AbsNameNum = i.ToString();
                    structPsgrInfant.PTC = "INF";
                    structPsgrInfant.TIC = "";
                    passengerType.PsgrAry.Add(structPsgrInfant);
                }

                //optimize1
                optimize1.RecType = "1001";
                optimize1.KlrID.Add("AAFI");

                //Optimize2
                optimize2.RecType = "1425";
                optimize2.KlrID.Add("EROR");
                optimize2.KlrID.Add("GFGQ");
                optimize2.KlrID.Add("GFXI");
                optimize2.KlrID.Add("GFPI");
                optimize2.KlrID.Add("GFRI");
                optimize2.KlrID.Add("GFJG");
                optimize2.KlrID.Add("GFMM");

                //genQuoteInfo
                genQuoteInfo.RulesProcess = "Y";
                genQuoteInfo.AltCurrency = ConfigurationManager.AppSettings["AltCurrency"].ToString();
                genQuoteInfo.EquivCurrency = ConfigurationManager.AppSettings["EquivCurrency"].ToString();

                //PFInfo
                pFInfo.ReqAirVPFs = "Y";
                PFInfo.PF structPF = new PFInfo.PF();
                structPF.StartODRange = "00";
                structPF.EndODRange = "00";
                structPF.CRS = "1G";
                structPF.PCC = ConfigurationManager.AppSettings["hcm_pcc"].ToString();
                structPF.AirV = "";
                structPF.Acct = "";
                structPF.PublishedFaresInd = "Y";
                structPF.Type = "A";
                structPF.AcctCodeRestrict = "";
                pFInfo.PFAry.Add(structPF);
                #endregion

                request = "<FareQuoteSuperBB_11>"
                                    + "<AirAvailMods>"
                                        + genAvailDeparture.GetXml()
                                    + "</AirAvailMods>"
                                    + "<AirAvailMods>"
                                        + genAvailReturn.GetXml()
                                    + "</AirAvailMods>"
                                    + "<SuperBBMods>"
                                        + passengerType.GetXml()
                                        + genQuoteInfo.GetXml()
                                        + pFInfo.GetXml()
                                        + optimize1.GetXml()
                                        + optimize2.GetXml()
                                    + "</SuperBBMods>"
                                + "</FareQuoteSuperBB_11>";
            }

            //Save to Xml files
            string fileName = GenerateResponseSearchKey(searchArchive, isOneway);
            string appPath = AppDomain.CurrentDomain.BaseDirectory;
            FileInfo fileInfo = new FileInfo(appPath + "/Request/" + fileName);
            if (fileInfo != null)
            {
                File.Delete(appPath + "/Request/" + fileName);
            }

            Common.SaveFile(appPath + "/Request/" + fileName, request);

            return request;
        }

        public string GenerateResponseSearchKey(tblFlightSearchArchive searchArchive, bool isOneway)
        {
            string fileName = "";
            if (isOneway)
            {
                fileName = ((searchArchive.ItineraryType.Value) ? "1-" : "0-")
                                + searchArchive.DepartureAirportCode + "-"
                                + searchArchive.DestinationAirportCode + "-"
                                + searchArchive.DepartureDate.Value.ToString("ddMMyyyy") + "-"
                                + "ADT" + searchArchive.Adult.ToString() + "-"
                                + "CHD" + searchArchive.Children.ToString() + "-"
                                + "INF" + searchArchive.Infant.ToString()
                                + ".xml";

            }
            else
            {
                fileName = ((searchArchive.ItineraryType.Value) ? "1-" : "0-")
                                + searchArchive.DepartureAirportCode + "-"
                                + searchArchive.DestinationAirportCode + "-"
                                + searchArchive.DepartureDate.Value.ToString("ddMMyyyy") + "-"
                                + searchArchive.ReturnDate.Value.ToString("ddMMyyyy") + "-"
                                + "ADT" + searchArchive.Adult.ToString() + "-"
                                + "CHD" + searchArchive.Children.ToString() + "-"
                                + "INF" + searchArchive.Infant.ToString()
                                + ".xml";
            }
            return fileName;
        }

        public double ConvertStringToDouble(string value)
        {
            CultureInfo culture = CultureInfo.CreateSpecificCulture("es-ES");
            try
            {
                return double.Parse(value, culture);
            }
            catch { return 0; }
        }

        //AirAvail
        public AvailFlt GetAvailFlt(XmlDocument doc, List<GeoData> listGeoData, List<tblAirline> listAirline, List<tblAircraft> listAircraft)
        {
            AvailFlt obj = null;
            try
            {
                obj = new AvailFlt();
                XmlNode node;

                node = doc.SelectSingleNode("//AvailFlt/AirV/text()");
                if (node != null)
                {
                    obj.AirV = node.Value;
                    obj.AirlineDetail = listAirline.FirstOrDefault(a => a.Code == obj.AirV);
                }

                node = doc.SelectSingleNode("//AvailFlt/FltNum/text()");
                if (node != null) { obj.FltNum = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/OpSuf/text()");
                if (node != null) { obj.OpSuf = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/StartDt/text()");
                if (node != null) { obj.StartDt = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/StartAirp/text()");
                if (node != null)
                {
                    obj.StartAirp = node.Value;
                    obj.StartPointAirport = listGeoData.FirstOrDefault(a => a.Code == obj.StartAirp).Airport;
                    obj.StartPointCity = listGeoData.FirstOrDefault(a => a.Code == obj.StartAirp).City;
                }

                node = doc.SelectSingleNode("//AvailFlt/EndAirp/text()");
                if (node != null)
                {
                    obj.EndAirp = node.Value;
                    obj.EndPointAirport = listGeoData.FirstOrDefault(a => a.Code == obj.EndAirp).Airport;
                    obj.EndPointCity = listGeoData.FirstOrDefault(a => a.Code == obj.EndAirp).City;
                }

                node = doc.SelectSingleNode("//AvailFlt/StartTm/text()");
                if (node != null) { obj.StartTm = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/EndTm/text()");
                if (node != null) { obj.EndTm = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/DayChg/text()");
                if (node != null) { obj.DayChg = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/Conx/text()");
                if (node != null) { obj.Conx = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/AirpChg/text()");
                if (node != null) { obj.AirpChg = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/Equip/text()");
                if (node != null)
                {
                    tblAircraft aircraft = listAircraft.FirstOrDefault(a => a.IATA == node.Value);
                    if (aircraft != null)
                    {
                        obj.Equip = aircraft.Manufacturer + " " + aircraft.Model;
                    }
                    else
                    {
                        obj.Equip = node.Value;
                    }
                }

                node = doc.SelectSingleNode("//AvailFlt/Spare1/text()");
                if (node != null) { obj.Spare1 = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/NumStops/text()");
                if (node != null) { obj.NumStops = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/OpAirVInd/text()");
                if (node != null) { obj.OpAirVInd = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/Perf/text()");
                if (node != null) { obj.Perf = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/LinkSellAgrmnt/text()");
                if (node != null) { obj.LinkSellAgrmnt = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/DispOption/text()");
                if (node != null) { obj.DispOption = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/InsideAvailOption/text()");
                if (node != null) { obj.InsideAvailOption = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/GenTrafRestriction/text()");
                if (node != null) { obj.GenTrafRestriction = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/DaysOperates/text()");
                if (node != null) { obj.DaysOperates = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/JrnyTm/text()");
                if (node != null) { obj.JrnyTm = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/EndDt/text()");
                if (node != null) { obj.EndDt = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/OpAirV/text()");
                if (node != null)
                {
                    obj.OpAirV = node.Value;
                    if (node.Value != "")
                    {
                        obj.OpAirlineDetail = listAirline.FirstOrDefault(a => a.Code == node.Value);
                    }
                    else
                    {
                        obj.OpAirlineDetail = obj.AirlineDetail;
                    }
                }
                else
                {
                    obj.OpAirlineDetail = obj.AirlineDetail;
                }

                node = doc.SelectSingleNode("//AvailFlt/OpFltDesignator/text()");
                if (node != null) { obj.OpFltDesignator = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/OpFltSuf/text()");
                if (node != null) { obj.OpFltSuf = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/StartTerminal/text()");
                if (node != null) { obj.StartTerminal = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/EndTerminal/text()");
                if (node != null) { obj.EndTerminal = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/FltTm/text()");
                if (node != null) { obj.FltTm = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/LSAInd/text()");
                if (node != null) { obj.LSAInd = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/GalileoAirVInd/text()");
                if (node != null) { obj.GalileoAirVInd = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/ETktEligibility/text()");
                if (node != null) { obj.ETktEligibility = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/ScheduleLevelCarrier/text()");
                if (node != null) { obj.ScheduleLevelCarrier = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/FrstDwnlnStp/text()");
                if (node != null)
                {
                    obj.FrstDwnlnStp = node.Value;
                    if (node.Value.Trim() != "")
                    {
                        obj.FrstDwnlnStpCity = listGeoData.FirstOrDefault(a => a.Code == node.Value).City;
                    }
                }

                node = doc.SelectSingleNode("//AvailFlt/LastDwnlnStp/text()");
                if (node != null)
                {
                    obj.LastDwnlnStp = node.Value;
                    if (node.Value.Trim() != "")
                    {
                        obj.LastDwnlnStpCity = listGeoData.FirstOrDefault(a => a.Code == node.Value).City;
                    }
                }

                node = doc.SelectSingleNode("//AvailFlt/SponsoredFltInd/text()");
                if (node != null) { obj.SponsoredFltInd = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/SponsoredFltLineNum/text()");
                if (node != null) { obj.SponsoredFltLineNum = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/NeutralFltLineNum/text()");
                if (node != null) { obj.NeutralFltLineNum = node.Value; }

                node = doc.SelectSingleNode("//AvailFlt/SponsoredFltKey/text()");
                if (node != null) { obj.SponsoredFltKey = node.Value; }

            }
            catch (Exception ex)
            {
                Common.WriteToFile(ex.ToString());
                return null;
            }
            return obj;
        }

        #region Get data from database after group code
        public List<tblAirline> GetListAirline(XmlNodeList nodeList)
        {
            List<string> listAirlineCode = new List<string>();
            foreach (XmlElement e in nodeList)
            {
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(e.OuterXml);
                XmlNode node;

                node = xml.SelectSingleNode("//AvailFlt/AirV/text()");
                if (node != null)
                {
                    listAirlineCode.Add(node.Value);
                }

                node = xml.SelectSingleNode("//AvailFlt/OpAirV/text()");
                if (node != null)
                {
                    listAirlineCode.Add(node.Value);
                }
            }

            var items = listAirlineCode.GroupBy(item => item).Select(g => new { key = g.Key }).ToList();
            List<tblAirline> listAirline = new List<tblAirline>();
            foreach (var code in items)
            {
                tblAirline obj = new AirlinesDB().GetByKey(code.key);
                if (obj != null)
                {
                    listAirline.Add(obj);
                }
            }
            return listAirline;
        }

        public List<tblAircraft> GetListAircraft(XmlNodeList nodeList)
        {
            List<string> listIATA = new List<string>();
            foreach (XmlElement e in nodeList)
            {
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(e.OuterXml);
                XmlNode node;

                node = xml.SelectSingleNode("//AvailFlt/Equip/text()");
                if (node != null)
                {
                    listIATA.Add(node.Value);
                }
            }

            var items = listIATA.GroupBy(item => item).Select(g => new { key = g.Key }).ToList();
            List<tblAircraft> listAircraft = new List<tblAircraft>();
            foreach (var code in items)
            {
                tblAircraft obj = new AircraftDB().GetByIATA(code.key);
                if (obj != null)
                {
                    listAircraft.Add(obj);
                }
                else
                {
                    obj = new tblAircraft();
                    obj.IATA = code.key;
                    obj.Model = code.key;
                    obj.Visible = true;
                    obj.Manufacturer = code.key;
                    listAircraft.Add(obj);
                }
            }
            return listAircraft;
        }

        public List<GeoData> GetListGeoData(XmlNodeList nodeList)
        {
            List<string> listAirportCode = new List<string>();
            foreach (XmlElement e in nodeList)
            {
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(e.OuterXml);
                XmlNode node;

                node = xml.SelectSingleNode("//AvailFlt/StartAirp/text()");
                if (node != null)
                {
                    listAirportCode.Add(node.Value);
                }

                node = xml.SelectSingleNode("//AvailFlt/EndAirp/text()");
                if (node != null)
                {
                    listAirportCode.Add(node.Value);
                }

                node = xml.SelectSingleNode("//AvailFlt/FrstDwnlnStp/text()");
                if (node != null)
                {
                    listAirportCode.Add(node.Value);
                }

                node = xml.SelectSingleNode("//AvailFlt/LastDwnlnStp/text()");
                if (node != null)
                {
                    listAirportCode.Add(node.Value);
                }
            }

            var items = listAirportCode.GroupBy(item => item).Select(g => new { key = g.Key }).ToList();

            List<GeoData> listGeoData = new List<GeoData>();
            foreach (var code in items)
            {
                GeoData item = new GeoData();
                item.Code = code.key;
                item.Airport = new GeoAirportDB().GetByKey(code.key);
                if (item.Airport != null)
                {
                    if (item.Airport.tblGeoCity != null)
                    {
                        item.City = item.Airport.tblGeoCity;
                        item.Country = item.City.tblGeoCountry;
                        item.Continent = item.Country.tblGeoContinent;
                    }
                    else
                    {
                        item.City = new tblGeoCity();
                        item.City.Code = item.Code;
                        item.City.Name_Vi = item.Code;
                        item.City.Name_En = item.Code;
                        item.City.Name_Fr = item.Code;

                        item.Country = new tblGeoCountry();
                        item.Continent = new tblGeoContinent();
                    }
                }
                listGeoData.Add(item);
            }
            return listGeoData;
        }
        #endregion

        public AirAvail GetAirAvail(XmlDocument doc)
        {
            AirAvail obj = new AirAvail();
            try
            {
                XmlNodeList nodeList = doc.SelectNodes("//AirAvail/AvailFlt");
                List<GeoData> listGeoData = GetListGeoData(nodeList);
                List<tblAirline> listAirline = GetListAirline(nodeList);
                List<tblAircraft> listAircraft = GetListAircraft(nodeList);

                foreach (XmlElement e in nodeList)
                {
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(e.OuterXml);
                    AvailFlt item = GetAvailFlt(xml, listGeoData, listAirline, listAircraft);
                    obj.ListAvailFlt.Add(item);
                }
            }
            catch
            {
                return null;
            }
            return obj;
        }

        public List<AirAvail> GetListAirAvail(XmlDocument doc)
        {
            List<AirAvail> list = new List<AirAvail>();
            try
            {
                XmlNodeList nodeList = doc.SelectNodes("//FareQuoteSuperBB_11/AirAvail");
                foreach (XmlElement e in nodeList)
                {
                    AirAvail obj = new AirAvail();
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(e.OuterXml);
                    AirAvail item = GetAirAvail(xml);
                    list.Add(item);
                }
            }
            catch
            {
                return list;
            }
            return list;
        }

        public AirAvail GetDepartureAirAvail(XmlDocument doc)
        {
            return GetListAirAvail(doc)[0];
        }

        public AirAvail GetReturnAirAvail(XmlDocument doc)
        {
            return GetListAirAvail(doc)[1];
        }

        //FareInfo
        public GenQuoteDetails GetGenQuoteDetails(XmlDocument doc)
        {
            GenQuoteDetails obj = null;
            try
            {
                obj = new GenQuoteDetails();
                XmlNode node;

                node = doc.SelectSingleNode("//GenQuoteDetails/UniqueKey/text()");
                if (node != null) { obj.UniqueKey = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/QuoteNum/text()");
                if (node != null) { obj.QuoteNum = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/QuoteType/text()");
                if (node != null) { obj.QuoteType = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/LastTkDt/text()");
                if (node != null) { obj.LastTkDt = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/QuoteDt/text()");
                if (node != null) { obj.QuoteDt = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/IntlSaleInd/text()");
                if (node != null) { obj.IntlSaleInd = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/BaseFareCurrency/text()");
                if (node != null) { obj.BaseFareCurrency = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/BaseFareAmt/text()");
                if (node != null) { obj.BaseFareAmt = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/LowestOrNUCFare/text()");
                if (node != null) { obj.LowestOrNUCFare = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/BaseDecPos/text()");
                if (node != null) { obj.BaseDecPos = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/EquivCurrency/text()");
                if (node != null) { obj.EquivCurrency = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/EquivAmt/text()");
                if (node != null) { obj.EquivAmt = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/EquivDecPos/text()");
                if (node != null) { obj.EquivDecPos = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/TotCurrency/text()");
                if (node != null) { obj.TotCurrency = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/TotAmt/text()");
                if (node != null) { obj.TotAmt = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/TotDecPos/text()");
                if (node != null) { obj.TotDecPos = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/ITNum/text()");
                if (node != null) { obj.ITNum = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/RteBasedQuote/text()");
                if (node != null) { obj.RteBasedQuote = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/M0/text()");
                if (node != null) { obj.M0 = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/M5/text()");
                if (node != null) { obj.M5 = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/M10/text()");
                if (node != null) { obj.M10 = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/M15/text()");
                if (node != null) { obj.M15 = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/M20/text()");
                if (node != null) { obj.M20 = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/M25/text()");
                if (node != null) { obj.M25 = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/Spare1/text()");
                if (node != null) { obj.Spare1 = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/PrivFQd/text()");
                if (node != null) { obj.PrivFQd = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/PFOverrides/text()");
                if (node != null) { obj.PFOverrides = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/FlatFQd/text()");
                if (node != null) { obj.FlatFQd = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/DirMinApplied/text()");
                if (node != null) { obj.DirMinApplied = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/VATIncInd/text()");
                if (node != null) { obj.VATIncInd = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/PenApplies/text()");
                if (node != null) { obj.PenApplies = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/Spare2/text()");
                if (node != null) { obj.Spare2 = node.Value; }

                node = doc.SelectSingleNode("//GenQuoteDetails/QuoteBasis/text()");
                if (node != null) { obj.QuoteBasis = node.Value; }

                XmlNodeList nodeList = doc.SelectNodes("//GenQuoteDetails/TaxDataAry/TaxData");
                foreach (XmlElement e in nodeList)
                {
                    GenQuoteDetails.TaxData TaxData = new GenQuoteDetails.TaxData();

                    node = e.SelectSingleNode("Country/text()");
                    if (node != null) { TaxData.Country = node.Value; }

                    node = e.SelectSingleNode("Amt/text()");
                    if (node != null) { TaxData.Amt = node.Value; }

                    obj.TaxDataAry.Add(TaxData);
                }
            }
            catch
            {
                return null;
            }
            return obj;
        }

        public PsgrTypes GetPsgrTypes(XmlDocument doc)
        {
            PsgrTypes obj = null;
            try
            {
                obj = new PsgrTypes();
                XmlNode node;

                node = doc.SelectSingleNode("//PsgrTypes/UniqueKey/text()");
                if (node != null) { obj.UniqueKey = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/PICReq/text()");
                if (node != null) { obj.PICReq = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/QueryAmt/text()");
                if (node != null) { obj.QueryAmt = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/QueryIATAFmt/text()");
                if (node != null) { obj.QueryIATAFmt = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/QueryApolloFmt/text()");
                if (node != null) { obj.QueryApolloFmt = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/PercentQuery/text()");
                if (node != null) { obj.PercentQuery = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/AmtQuery/text()");
                if (node != null) { obj.AmtQuery = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/AgeQuery/text()");
                if (node != null) { obj.AgeQuery = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/ReqReturnedPIC/text()");
                if (node != null) { obj.ReqReturnedPIC = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/QuoteOnlyPICReq/text()");
                if (node != null) { obj.QuoteOnlyPICReq = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/HasDiscData/text()");
                if (node != null) { obj.HasDiscData = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/RespPIC/text()");
                if (node != null) { obj.RespPIC = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/RespAmt/text()");
                if (node != null) { obj.RespAmt = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/RespIATAFmt/text()");
                if (node != null) { obj.RespIATAFmt = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/RespATPCOFmt/text()");
                if (node != null) { obj.RespATPCOFmt = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/PercentResp/text()");
                if (node != null) { obj.PercentResp = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/AmtResp/text()");
                if (node != null) { obj.AmtResp = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/AgeResp/text()");
                if (node != null) { obj.AgeResp = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/PFCsApply/text()");
                if (node != null) { obj.PFCsApply = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/NPFReq/text()");
                if (node != null) { obj.NPFReq = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/Spare1/text()");
                if (node != null) { obj.Spare1 = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/PICPsgrs/text()");
                if (node != null) { obj.PICPsgrs = node.Value; }

                node = doc.SelectSingleNode("//PsgrTypes/PsgrNum/text()");
                if (node != null) { obj.PsgrNum = node.Value; }
            }
            catch
            {
                return null;
            }
            return obj;
        }

        public FlightItemCrossRef GetFlightItemCrossRef(XmlDocument doc)
        {
            FlightItemCrossRef obj = null;
            try
            {
                obj = new FlightItemCrossRef();
                XmlNode node;

                node = doc.SelectSingleNode("//FlightItemCrossRef/UniqueKey/text()");
                if (node != null) { obj.UniqueKey = node.Value; }

                node = doc.SelectSingleNode("//FlightItemCrossRef/ODNum/text()");
                if (node != null) { obj.ODNum = node.Value; }

                node = doc.SelectSingleNode("//FlightItemCrossRef/ODNumLegs/text()");
                if (node != null) { obj.ODNumLegs = node.Value; }

                XmlNodeList nodeList = doc.SelectNodes("//FlightItemCrossRef/FltItemAry/FltItem");
                foreach (XmlElement e in nodeList)
                {
                    FlightItemCrossRef.FltItem FltItem = new FlightItemCrossRef.FltItem();
                    FltItem.BICAry = new List<FlightItemCrossRef.BICInfo>();

                    node = e.SelectSingleNode("IndexNum/text()");
                    if (node != null) { FltItem.IndexNum = node.Value; }

                    XmlNodeList BICInfoNodeList = e.SelectNodes(".//BICAry/BICInfo");
                    foreach (XmlElement BICInfoElement in BICInfoNodeList)
                    {
                        FlightItemCrossRef.BICInfo BICInfo = new FlightItemCrossRef.BICInfo();
                        BICInfo.PsgrDescNumAry = new List<string>();

                        node = BICInfoElement.SelectSingleNode("BIC/text()");
                        if (node != null) { BICInfo.BIC = node.Value; }

                        XmlNodeList NumNodeList = BICInfoElement.SelectNodes(".//PsgrDescNumAry/Num");
                        foreach (XmlElement NumElement in NumNodeList)
                        {
                            BICInfo.PsgrDescNumAry.Add(NumElement.InnerText);
                        }

                        FltItem.BICAry.Add(BICInfo);
                    }

                    obj.FltItemAry.Add(FltItem);
                }
            }
            catch
            {
                return null;
            }
            return obj;
        }

        public RulesInfo GetRulesInfo(XmlDocument doc)
        {
            RulesInfo obj = null;
            try
            {
                obj = new RulesInfo();
                XmlNode node;

                node = doc.SelectSingleNode("//RulesInfo/AirV/text()");
                if (node != null) { obj.AirV = node.Value; }

                //Tạm thời chỉ lấy AirV
            }
            catch
            {
                return null;
            }
            return obj;
        }

        public PenaltyRules GetPenaltyRules(XmlDocument doc)
        {
            PenaltyRules obj = null;
            try
            {
                obj = new PenaltyRules();
                XmlNode node;

                node = doc.SelectSingleNode("//PenaltyRules/UniqueKey/text()");
                if (node != null) { obj.UniqueKey = node.Value; }

                node = doc.SelectSingleNode("//PenaltyRules/QuoteNum/text()");
                if (node != null) { obj.QuoteNum = node.Value; }

                node = doc.SelectSingleNode("//PenaltyRules/FareComponentNum/text()");
                if (node != null) { obj.FareComponentNum = node.Value; }

                node = doc.SelectSingleNode("//PenaltyRules/PriceUnitNum/text()");
                if (node != null) { obj.PriceUnitNum = node.Value; }

                XmlNodeList nodeList = doc.SelectNodes("//PenaltyRules/DepRequiredAry/DepRequiredAryItem");
                foreach (XmlElement e in nodeList)
                {
                    PenaltyRules.DepRequiredAryItem item = new PenaltyRules.DepRequiredAryItem();

                    node = e.SelectSingleNode("DepRequired/text()");
                    if (node != null) { item.DepRequired = node.Value; }

                    node = e.SelectSingleNode("DepNonRef/text()");
                    if (node != null) { item.DepNonRef = node.Value; }

                    node = e.SelectSingleNode("TkNonRef/text()");
                    if (node != null) { item.TkNonRef = node.Value; }

                    node = e.SelectSingleNode("AirVFee/text()");
                    if (node != null) { item.AirVFee = node.Value; }

                    node = e.SelectSingleNode("Cancellation/text()");
                    if (node != null) { item.Cancellation = node.Value; }

                    node = e.SelectSingleNode("FailConfirmSpace/text()");
                    if (node != null) { item.FailConfirmSpace = node.Value; }

                    node = e.SelectSingleNode("ItinChg/text()");
                    if (node != null) { item.ItinChg = node.Value; }

                    node = e.SelectSingleNode("ReplaceTk/text()");
                    if (node != null) { item.ReplaceTk = node.Value; }

                    node = e.SelectSingleNode("Applicable/text()");
                    if (node != null) { item.Applicable = node.Value; }

                    node = e.SelectSingleNode("ApplicableTo/text()");
                    if (node != null) { item.ApplicableTo = node.Value; }

                    node = e.SelectSingleNode("Spare1/text()");
                    if (node != null) { item.Spare1 = node.Value; }

                    node = e.SelectSingleNode("Spare2/text()");
                    if (node != null) { item.Spare2 = node.Value; }

                    node = e.SelectSingleNode("Spare3/text()");
                    if (node != null) { item.Spare3 = node.Value; }

                    node = e.SelectSingleNode("Spare4/text()");
                    if (node != null) { item.Spare4 = node.Value; }

                    node = e.SelectSingleNode("Spare5/text()");
                    if (node != null) { item.Spare5 = node.Value; }

                    node = e.SelectSingleNode("Spare6/text()");
                    if (node != null) { item.Spare6 = node.Value; }

                    node = e.SelectSingleNode("Amt/text()");
                    if (node != null) { item.Amt = node.Value; }

                    node = e.SelectSingleNode("Type/text()");
                    if (node != null) { item.Type = node.Value; }

                    node = e.SelectSingleNode("Crncy/text()");
                    if (node != null) { item.Crncy = node.Value; }

                    obj.DepRequiredAry.Add(item);
                }
            }
            catch
            {
                return null;
            }
            return obj;
        }

        public InfoMsg GetInfoMsg(XmlDocument doc)
        {
            InfoMsg obj = null;
            try
            {
                obj = new InfoMsg();
                XmlNode node;

                node = doc.SelectSingleNode("//InfoMsg/UniqueKey/text()");
                if (node != null) { obj.UniqueKey = node.Value; }

                node = doc.SelectSingleNode("//InfoMsg/QuoteNum/text()");
                if (node != null) { obj.QuoteNum = node.Value; }

                node = doc.SelectSingleNode("//InfoMsg/MsgNum/text()");
                if (node != null) { obj.MsgNum = node.Value; }

                node = doc.SelectSingleNode("//InfoMsg/AppNum/text()");
                if (node != null) { obj.AppNum = node.Value; }

                node = doc.SelectSingleNode("//InfoMsg/MsgType/text()");
                if (node != null) { obj.MsgType = node.Value; }

                node = doc.SelectSingleNode("//InfoMsg/Lang/text()");
                if (node != null) { obj.Lang = node.Value; }

                node = doc.SelectSingleNode("//InfoMsg/Text/text()");
                if (node != null) { obj.Text = node.Value; }
            }
            catch
            {
                return null;
            }
            return obj;
        }

        public FareInfo GetFareInfo(XmlDocument doc)
        {
            FareInfo obj = new FareInfo();
            try
            {
                XmlNodeList nodeList;

                //Get list GenQuoteDetails
                nodeList = doc.SelectNodes("//FareInfo/GenQuoteDetails");
                foreach (XmlElement e in nodeList)
                {
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(e.OuterXml);
                    GenQuoteDetails item = GetGenQuoteDetails(xml);
                    obj.ListGenQuoteDetails.Add(item);
                }

                //Get list PsgrTypes
                nodeList = doc.SelectNodes("//FareInfo/PsgrTypes");
                foreach (XmlElement e in nodeList)
                {
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(e.OuterXml);
                    PsgrTypes item = GetPsgrTypes(xml);
                    obj.ListPsgrTypes.Add(item);
                }

                //Get list FlightItemCrossRef
                nodeList = doc.SelectNodes("//FareInfo/FlightItemCrossRef");
                foreach (XmlElement e in nodeList)
                {
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(e.OuterXml);
                    FlightItemCrossRef item = GetFlightItemCrossRef(xml);
                    obj.ListFlightItemCrossRef.Add(item);
                }

                //Get list RulesInfo
                nodeList = doc.SelectNodes("//FareInfo/RulesInfo");
                foreach (XmlElement e in nodeList)
                {
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(e.OuterXml);
                    XmlNode node = xml.SelectSingleNode("//RulesInfo/FareNum/text()");
                    if (node != null)
                    {
                        if (node.Value.Equals("1"))
                        {
                            obj.ListDepartureRulesInfo.Add(e.OuterXml);
                            obj.DepartRulesInfo = GetRulesInfo(xml);
                        }
                        else
                        {
                            obj.ListReturnRulesInfo.Add(e.OuterXml);
                            obj.ReturnRulesInfo = GetRulesInfo(xml);
                        }
                    }
                }

                //Get list PenaltyRules
                nodeList = doc.SelectNodes("//FareInfo/PenaltyRules");
                foreach (XmlElement e in nodeList)
                {
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(e.OuterXml);
                    PenaltyRules item = GetPenaltyRules(xml);
                    obj.ListPenaltyRules.Add(item);
                }

                //Get list InfoMsg
                nodeList = doc.SelectNodes("//FareInfo/InfoMsg");
                foreach (XmlElement e in nodeList)
                {
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(e.OuterXml);
                    InfoMsg item = GetInfoMsg(xml);
                    obj.ListInfoMsg.Add(item);
                }
            }
            catch
            {
                return null;
            }
            return obj;
        }

        public List<FareInfo> GetListFareInfo(XmlDocument doc)
        {
            List<FareInfo> list = new List<FareInfo>();
            try
            {
                XmlNodeList nodeList = doc.SelectNodes("//FareQuoteSuperBB_11/FareInfo");
                foreach (XmlElement e in nodeList)
                {
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(e.OuterXml);
                    FareInfo item = GetFareInfo(xml);
                    list.Add(item);
                }
            }
            catch
            {
                return list;
            }
            return list;
        }

        //GetData
        public List<FareData> GetFareData(XmlDocument responseXml)
        {
            List<FareData> listData = new List<FareData>();
            try
            {
                string currency = ConfigurationManager.AppSettings["default_currency"].ToString();
                Common utility = new Common();
                GDSParser parser = new GDSParser();
                List<AirAvail> listAirAvail = parser.GetListAirAvail(responseXml);
                AirAvail departureAirAvail = listAirAvail[0];
                AirAvail returnAirAvail = new AirAvail();
                if (listAirAvail.Count > 1)
                {
                    //Round trip
                    returnAirAvail = listAirAvail[1];
                }
                List<FareInfo> listFareInfo = parser.GetListFareInfo(responseXml);

                int index = 0;
                foreach (FareInfo fareInfo in listFareInfo)
                {
                    FareData data = new FareData();
                    data.FareDataId = index;
                    data.FareInfo = fareInfo;
                    string inputCurrency = "";
                    data.CurrencyCode = currency;
                    data.IBE = "galileo";

                    #region PlatingCarrier
                    foreach (InfoMsg infoMsg in fareInfo.ListInfoMsg)
                    {
                        if (infoMsg.Text.Contains("DEFAULT PLATING CARRIER"))
                        {
                            string[] array = infoMsg.Text.Split(' ');
                            data.PlatingCarrier = array[array.Length - 1].Trim();
                            break;
                        }
                    }
                    if (string.IsNullOrEmpty(data.PlatingCarrier))
                    {
                        data.PlatingCarrier = fareInfo.DepartRulesInfo.AirV;
                    }
                    #endregion

                    #region Fare
                    foreach (PsgrTypes obj in fareInfo.ListPsgrTypes)
                    {
                        if (obj.PICReq.Equals("AD"))
                        {
                            data.TotalAdult = Convert.ToInt32(obj.PICPsgrs);
                            foreach (GenQuoteDetails genQuoteDetails in fareInfo.ListGenQuoteDetails)
                            {
                                if (Convert.ToInt32(genQuoteDetails.UniqueKey) == Convert.ToInt32(obj.UniqueKey))
                                {
                                    data.FareAdult = Common.MoneyExchange(Convert.ToDouble(genQuoteDetails.TotAmt) / Math.Pow(10, Convert.ToDouble(genQuoteDetails.TotDecPos)), genQuoteDetails.TotCurrency, currency);
                                }

                                inputCurrency = genQuoteDetails.TotCurrency;
                            }
                        }

                        if (obj.PICReq.Equals("CNN"))
                        {
                            data.TotalChild = Convert.ToInt32(obj.PICPsgrs);
                            foreach (GenQuoteDetails genQuoteDetails in fareInfo.ListGenQuoteDetails)
                            {
                                if (Convert.ToInt32(genQuoteDetails.UniqueKey) == Convert.ToInt32(obj.UniqueKey))
                                {
                                    data.FareChild = Convert.ToDouble(genQuoteDetails.TotAmt) / Math.Pow(10, Convert.ToDouble(genQuoteDetails.TotDecPos));
                                    data.FareChild = Common.MoneyExchange(Convert.ToDouble(genQuoteDetails.TotAmt) / Math.Pow(10, Convert.ToDouble(genQuoteDetails.TotDecPos)), genQuoteDetails.TotCurrency, currency);
                                }
                            }
                        }

                        if (obj.PICReq.Equals("INF"))
                        {
                            data.TotalInfant = Convert.ToInt32(obj.PICPsgrs);
                            foreach (GenQuoteDetails genQuoteDetails in fareInfo.ListGenQuoteDetails)
                            {
                                if (Convert.ToInt32(genQuoteDetails.UniqueKey) == Convert.ToInt32(obj.UniqueKey))
                                {
                                    data.FareInfant = Convert.ToDouble(genQuoteDetails.TotAmt) / Math.Pow(10, Convert.ToDouble(genQuoteDetails.TotDecPos));
                                    data.FareInfant = Common.MoneyExchange(Convert.ToDouble(genQuoteDetails.TotAmt) / Math.Pow(10, Convert.ToDouble(genQuoteDetails.TotDecPos)), genQuoteDetails.TotCurrency, currency);
                                }
                            }
                        }
                    }

                    data.TotalPax = data.TotalAdult + data.TotalChild + data.TotalInfant;
                    data.TotalFare = data.TotalAdult * data.FareAdult + data.TotalChild * data.FareChild + data.TotalInfant * data.FareInfant;
                    #endregion

                    List<PenaltyRules> listPenaltyRulesDeparture = (from a in fareInfo.ListPenaltyRules where a.FareComponentNum == "1" select a).ToList();
                    List<PenaltyRules> listPenaltyRulesReturn = (from a in fareInfo.ListPenaltyRules where a.FareComponentNum == "2" select a).ToList();

                    data.ListDepartureRulesInfo = fareInfo.ListDepartureRulesInfo;
                    data.ListReturnRulesInfo = fareInfo.ListReturnRulesInfo;

                    #region Departure
                    data.ListDepartureFlight = new List<Flight>();
                    int segmentNum = Convert.ToInt32(Convert.ToInt32(fareInfo.ListFlightItemCrossRef[0].ODNumLegs)); //Số segment cho 1 option

                    List<AvailFlt> listAvailFlt = new List<AvailFlt>();
                    foreach (FlightItemCrossRef.FltItem fltItem in fareInfo.ListFlightItemCrossRef[0].FltItemAry)
                    {
                        AvailFlt availFlt = departureAirAvail.ListAvailFlt[Convert.ToInt32(fltItem.IndexNum) - 1].ShallowCopy();
                        if (fltItem.BICAry.Count == 1)
                        {
                            availFlt.ClassAdult = fltItem.BICAry[0].BIC;
                            if (data.TotalChild > 0)
                            {
                                availFlt.ClassChild = fltItem.BICAry[0].BIC;
                            }
                            if (data.TotalInfant > 0)
                            {
                                availFlt.ClassInfant = fltItem.BICAry[0].BIC;
                            }
                        }
                        else
                        {
                            foreach (FlightItemCrossRef.BICInfo BICInfo in fltItem.BICAry)
                            {
                                if (BICInfo.PsgrDescNumAry.Count == 0)
                                {
                                    availFlt.ClassAdult = BICInfo.BIC;
                                    if (data.TotalChild > 0)
                                    {
                                        availFlt.ClassChild = BICInfo.BIC;
                                    }
                                    if (data.TotalInfant > 0)
                                    {
                                        availFlt.ClassInfant = BICInfo.BIC;
                                    }
                                }
                                else
                                {
                                    foreach (string num in BICInfo.PsgrDescNumAry)
                                    {
                                        int PsgrTypesIndex = Convert.ToInt32(num) - 1;
                                        if (fareInfo.ListPsgrTypes[PsgrTypesIndex].PICReq.Equals("AD"))
                                        {
                                            availFlt.ClassAdult = BICInfo.BIC;
                                        }
                                        else if (fareInfo.ListPsgrTypes[PsgrTypesIndex].PICReq.Equals("CNN"))
                                        {
                                            availFlt.ClassChild = BICInfo.BIC;
                                        }
                                        else if (fareInfo.ListPsgrTypes[PsgrTypesIndex].PICReq.Equals("INF"))
                                        {
                                            availFlt.ClassInfant = BICInfo.BIC;
                                        }
                                    }
                                }
                            }
                        }

                        availFlt.Class = ConvertClass(availFlt.ClassAdult);

                        listAvailFlt.Add(availFlt);
                    }

                    List<List<AvailFlt>> listlist = utility.Chunk(listAvailFlt, segmentNum);
                    int count = 0;
                    foreach (List<AvailFlt> item in listlist)
                    {
                        Flight flight = new Flight();
                        flight.FareDataId = index;
                        if (count == 0)
                        {
                            flight.Checked = "checked";
                        }
                        else
                        {
                            flight.Checked = "";
                        }
                        flight.Index = count;
                        flight.ListAvailFlt = item;

                        //Airline
                        flight.AirlineDetail = item[0].AirlineDetail;

                        //Start
                        flight.StartPointAirport = item[0].StartPointAirport;
                        flight.StartPointCity = item[0].StartPointCity;
                        flight.StartTm = utility.GetDate(item[0].StartDt, item[0].StartTm);

                        //End
                        flight.EndPointAirport = item[segmentNum - 1].EndPointAirport;
                        flight.EndPointCity = item[segmentNum - 1].EndPointCity;
                        flight.EndTm = utility.GetDate(item[segmentNum - 1].EndDt, item[segmentNum - 1].EndTm);

                        flight.Duration = TimeSpan.FromMinutes(Convert.ToInt32(item[0].JrnyTm));
                        data.TotalDuration = flight.Duration.TotalMinutes;

                        //StopTime
                        for (int i = 0; i < item.Count - 1; i++)
                        {
                            item[i].StopTime = utility.GetDate(item[i + 1].StartDt, item[i + 1].StartTm) - utility.GetDate(item[i].EndDt, item[i].EndTm);
                            if (utility.GetDate(item[i + 1].StartDt, item[i + 1].StartTm).Date != utility.GetDate(item[i].EndDt, item[i].EndTm).Date)
                            {
                                item[i].StopOvernight = "1";
                            }

                            //ChangeStation
                            if (!item[i].EndTerminal.Equals(item[i + 1].StartTerminal))
                            {
                                item[i].ChangeStation = "1";
                            }

                            //ChangeAirport
                            if (!item[i].EndAirp.Equals(item[i + 1].StartAirp))
                            {
                                item[i].ChangeAirport = item[i].EndPointAirport.Name_Vi + " → " + item[i + 1].StartPointAirport.Name_Vi;
                            }
                        }

                        //StopNum
                        flight.StopNum = segmentNum - 1;

                        //HasDownStop
                        for (int i = 0; i < item.Count; i++)
                        {
                            if (item[i].FrstDwnlnStp != "" || item[i].LastDwnlnStp != "")
                            {
                                flight.HasDownStop = "1";
                            }
                        }

                        //Check Non refund
                        foreach (PenaltyRules rules in listPenaltyRulesDeparture)
                        {
                            foreach (PenaltyRules.DepRequiredAryItem depRequiredAryItem in rules.DepRequiredAry)
                            {
                                if (depRequiredAryItem.TkNonRef.Equals("Y"))
                                {
                                    flight.NoRefund = true;
                                    break;
                                }
                            }
                        }

                        item[segmentNum - 1].IsLastItem = "1";
                        data.ListDepartureFlight.Add(flight);
                        count++;
                    }

                    data.DepartureDate = utility.GetDate(data.ListDepartureFlight[0].ListAvailFlt[0].StartDt, data.ListDepartureFlight[0].ListAvailFlt[0].StartTm);
                    #endregion

                    #region Return
                    if (listAirAvail.Count > 1)
                    {
                        data.HasReturn = true;
                        data.ListReturnFlight = new List<Flight>();
                        segmentNum = Convert.ToInt32(Convert.ToInt32(fareInfo.ListFlightItemCrossRef[1].ODNumLegs));

                        listAvailFlt = new List<AvailFlt>();
                        foreach (FlightItemCrossRef.FltItem fltItem in fareInfo.ListFlightItemCrossRef[1].FltItemAry)
                        {
                            AvailFlt availFlt = returnAirAvail.ListAvailFlt[Convert.ToInt32(fltItem.IndexNum) - 1].ShallowCopy();
                            if (fltItem.BICAry.Count == 1)
                            {
                                availFlt.ClassAdult = fltItem.BICAry[0].BIC;
                                if (data.TotalChild > 0)
                                {
                                    availFlt.ClassChild = fltItem.BICAry[0].BIC;
                                }
                                if (data.TotalInfant > 0)
                                {
                                    availFlt.ClassInfant = fltItem.BICAry[0].BIC;
                                }
                            }
                            else
                            {
                                foreach (FlightItemCrossRef.BICInfo BICInfo in fltItem.BICAry)
                                {
                                    if (BICInfo.PsgrDescNumAry.Count == 0)
                                    {
                                        availFlt.ClassAdult = BICInfo.BIC;
                                        if (data.TotalChild > 0)
                                        {
                                            availFlt.ClassChild = BICInfo.BIC;
                                        }
                                        if (data.TotalInfant > 0)
                                        {
                                            availFlt.ClassInfant = BICInfo.BIC;
                                        }
                                    }
                                    else
                                    {
                                        foreach (string num in BICInfo.PsgrDescNumAry)
                                        {
                                            int PsgrTypesIndex = Convert.ToInt32(num) - 1;
                                            if (fareInfo.ListPsgrTypes[PsgrTypesIndex].PICReq.Equals("AD"))
                                            {
                                                availFlt.ClassAdult = BICInfo.BIC;
                                            }
                                            else if (fareInfo.ListPsgrTypes[PsgrTypesIndex].PICReq.Equals("CNN"))
                                            {
                                                availFlt.ClassChild = BICInfo.BIC;
                                            }
                                            else if (fareInfo.ListPsgrTypes[PsgrTypesIndex].PICReq.Equals("INF"))
                                            {
                                                availFlt.ClassInfant = BICInfo.BIC;
                                            }
                                        }
                                    }
                                }
                            }

                            availFlt.Class = ConvertClass(availFlt.ClassAdult);

                            listAvailFlt.Add(availFlt);
                        }

                        listlist = utility.Chunk(listAvailFlt, segmentNum);
                        count = 0;
                        foreach (List<AvailFlt> item in listlist)
                        {
                            Flight flight = new Flight();
                            flight.FareDataId = index;
                            if (count == 0)
                            {
                                flight.Checked = "checked";
                            }
                            else
                            {
                                flight.Checked = "";
                            }
                            flight.Index = count;
                            flight.ListAvailFlt = item;

                            //Airline
                            flight.AirlineDetail = item[0].AirlineDetail;

                            //Start
                            flight.StartPointAirport = item[0].StartPointAirport;
                            flight.StartPointCity = item[0].StartPointCity;
                            flight.StartTm = utility.GetDate(item[0].StartDt, item[0].StartTm);

                            //End
                            flight.EndPointAirport = item[segmentNum - 1].EndPointAirport;
                            flight.EndPointCity = item[segmentNum - 1].EndPointCity;
                            flight.EndTm = utility.GetDate(item[segmentNum - 1].EndDt, item[segmentNum - 1].EndTm);

                            flight.Duration = TimeSpan.FromMinutes(Convert.ToInt32(item[0].JrnyTm));
                            data.TotalDuration += flight.Duration.TotalMinutes;

                            //StopTime
                            for (int i = 0; i < item.Count - 1; i++)
                            {
                                item[i].StopTime = utility.GetDate(item[i + 1].StartDt, item[i + 1].StartTm) - utility.GetDate(item[i].EndDt, item[i].EndTm);
                                if (utility.GetDate(item[i + 1].StartDt, item[i + 1].StartTm).Date != utility.GetDate(item[i].EndDt, item[i].EndTm).Date)
                                {
                                    item[i].StopOvernight = "1";
                                }

                                //ChangeStation
                                if (!item[i].EndTerminal.Equals(item[i + 1].StartTerminal))
                                {
                                    item[i].ChangeStation = "1";
                                }

                                //ChangeAirport
                                if (!item[i].EndAirp.Equals(item[i + 1].StartAirp))
                                {
                                    item[i].ChangeAirport = item[i].EndPointAirport.Name_Vi + " → " + item[i + 1].StartPointAirport.Name_Vi;
                                }

                                //HasDownStop
                                if (item[i].FrstDwnlnStp != "" || item[i].LastDwnlnStp != "")
                                {
                                    flight.HasDownStop = "1";
                                    item[segmentNum - 1].IsLastItem = "1";
                                }
                            }

                            //StopNum
                            flight.StopNum = segmentNum - 1;

                            //HasDownStop
                            for (int i = 0; i < item.Count; i++)
                            {
                                if (item[i].FrstDwnlnStp != "" || item[i].LastDwnlnStp != "")
                                {
                                    flight.HasDownStop = "1";
                                }
                            }

                            //Check Non refund
                            foreach (PenaltyRules rules in listPenaltyRulesReturn)
                            {
                                foreach (PenaltyRules.DepRequiredAryItem depRequiredAryItem in rules.DepRequiredAry)
                                {
                                    if (depRequiredAryItem.TkNonRef.Equals("Y"))
                                    {
                                        flight.NoRefund = true;
                                        break;
                                    }
                                }
                            }

                            item[segmentNum - 1].IsLastItem = "1";
                            data.ListReturnFlight.Add(flight);
                            count++;
                        }

                        data.ReturnDate = utility.GetDate(data.ListReturnFlight[0].ListAvailFlt[0].StartDt, data.ListReturnFlight[0].ListAvailFlt[0].StartTm);
                    }
                    else
                    {
                        data.HasReturn = false;
                    }
                    #endregion

                    #region Fee
                    try
                    {
                        tblFee adtFee = new FeeDB().SearchFee(data.HasReturn ? 2 : 1, data.PlatingCarrier, "GDS", data.ListDepartureFlight[0].EndPointCity.tblGeoCountry.tblGeoContinent.Code, "ADT");
                        if (adtFee != null)
                        {
                            data.FeeAdult = adtFee.IsAmount.Value ? adtFee.Fee.Value : adtFee.Fee.Value * data.FareAdult / 100;
                            data.FeeAdult = Common.MoneyExchange(data.FeeAdult, inputCurrency, currency);
                        }
                        else
                        {
                            data.FeeAdult = (data.HasReturn ? ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_adt_rt"].ToString()) : ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_adt_ow"].ToString())) * data.FareAdult / 100;
                        }

                        tblFee chdFee = new FeeDB().SearchFee(data.HasReturn ? 2 : 1, data.PlatingCarrier, "GDS", data.ListDepartureFlight[0].EndPointCity.tblGeoCountry.tblGeoContinent.Code, "CHD");
                        if (chdFee != null)
                        {
                            data.FeeChild = chdFee.IsAmount.Value ? chdFee.Fee.Value : chdFee.Fee.Value * data.FareChild / 100;
                            data.FeeChild = Common.MoneyExchange(data.FeeChild, inputCurrency, currency);
                        }
                        else
                        {
                            data.FeeChild = (data.HasReturn ? ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_chd_rt"].ToString()) : ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_chd_ow"].ToString())) * data.FareChild / 100;
                        }

                        tblFee infFee = new FeeDB().SearchFee(data.HasReturn ? 2 : 1, data.PlatingCarrier, "GDS", data.ListDepartureFlight[0].EndPointCity.tblGeoCountry.tblGeoContinent.Code, "INF");
                        if (infFee != null)
                        {
                            data.FeeInfant = infFee.IsAmount.Value ? infFee.Fee.Value : infFee.Fee.Value * data.FareInfant / 100;
                            data.FeeInfant = Common.MoneyExchange(data.FeeInfant, inputCurrency, currency);
                        }
                        else
                        {
                            data.FeeInfant = (data.HasReturn ? ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_inf_rt"].ToString()) : ConvertStringToDouble(ConfigurationManager.AppSettings["defaul_fee_inf_ow"].ToString())) * data.FareInfant / 100;
                        }

                        data.TotalFee = data.TotalAdult * data.FeeAdult + data.TotalChild * data.FeeChild + data.TotalInfant * data.FeeInfant;
                    }
                    catch (Exception ex)
                    {
                        Common.WriteToFile(ex.ToString());
                    }
                    
                    #endregion

                    listData.Add(data);
                    index++;
                }
            }
            catch (Exception ex)
            {
                Common.WriteToFile(ex.ToString());
                return listData;
            }

            return listData;
        }

        public List<FareData> FilterData(List<FareData> listFareDataInput)
        {
            List<FareData> listFareData = new List<FareData>();
            foreach (FareData fareData in listFareDataInput)
            {
                FareData tempFareData = fareData.ShallowCopy();
                bool hasChangedClass = false;
                tempFareData.ListDepartureFlight = new List<Flight>();
                tempFareData.ListReturnFlight = new List<Flight>();
                if (fareData.TotalChild > 0)
                {
                    //Có CNN
                    if (fareData.TotalInfant == 0)
                    {
                        //Chỉ có ADT & CNN
                        foreach (Flight flight in fareData.ListDepartureFlight) //Chuyến đi
                        {
                            bool isRemove = false;
                            foreach (AvailFlt availFlt in flight.ListAvailFlt)
                            {
                                if (availFlt.ClassAdult != availFlt.ClassChild)
                                {
                                    //Nếu class ADT khác class CNN thì bỏ qua chặng này
                                    isRemove = true;
                                    break;
                                }
                            }

                            if (!isRemove)
                            {
                                tempFareData.ListDepartureFlight.Add(flight);
                            }
                        }

                        if (fareData.ListReturnFlight != null)
                        {
                            foreach (Flight flight in fareData.ListReturnFlight) //Chuyến về
                            {
                                bool isRemove = false;
                                foreach (AvailFlt availFlt in flight.ListAvailFlt)
                                {
                                    if (availFlt.ClassAdult != availFlt.ClassChild)
                                    {
                                        //Nếu class ADT khác class CNN thì bỏ qua chặng này
                                        isRemove = true;
                                        break;
                                    }
                                }

                                if (!isRemove)
                                {
                                    tempFareData.ListReturnFlight.Add(flight);
                                }
                            }
                        }
                    }
                    else
                    {
                        //Có cả ADT, CNN & INF
                        foreach (Flight flight in fareData.ListDepartureFlight) //Chuyến đi
                        {
                            bool isRemove = false;
                            foreach (AvailFlt availFlt in flight.ListAvailFlt)
                            {
                                if ((availFlt.ClassAdult != availFlt.ClassChild) && (availFlt.ClassAdult != availFlt.ClassInfant) && (availFlt.ClassChild != availFlt.ClassInfant))
                                {
                                    //Nếu class ADT, CNN & INF khác nhau thì bỏ qua chặng này
                                    isRemove = true;
                                    break;
                                }
                                else if ((availFlt.ClassAdult == availFlt.ClassChild) && (availFlt.ClassChild != availFlt.ClassInfant))
                                {
                                    //Nếu class ADT & CNN giống nhau, nhưng class INF khác CNN thì chuyển INF thành CNN
                                    availFlt.ClassInfant = availFlt.ClassChild;
                                    tempFareData.FareInfant = tempFareData.FareChild;
                                    tempFareData.TotalPax = tempFareData.TotalAdult + tempFareData.TotalChild + tempFareData.TotalInfant;
                                    hasChangedClass = true;
                                }
                            }

                            if (!isRemove)
                            {
                                tempFareData.ListDepartureFlight.Add(flight);
                            }
                        }

                        if (fareData.ListReturnFlight != null)
                        {
                            foreach (Flight flight in fareData.ListReturnFlight) //Chuyến về
                            {
                                bool isRemove = false;
                                foreach (AvailFlt availFlt in flight.ListAvailFlt)
                                {
                                    if ((availFlt.ClassAdult != availFlt.ClassChild) && (availFlt.ClassAdult != availFlt.ClassInfant) && (availFlt.ClassChild != availFlt.ClassInfant))
                                    {
                                        //Nếu class ADT, CNN & INF khác nhau thì bỏ qua chặng này
                                        isRemove = true;
                                        break;
                                    }
                                    else if ((availFlt.ClassAdult == availFlt.ClassChild) && (availFlt.ClassChild != availFlt.ClassInfant))
                                    {
                                        //Nếu class ADT & CNN giống nhau, nhưng class INF khác CNN thì chuyển INF thành CNN
                                        availFlt.ClassInfant = availFlt.ClassChild;
                                        tempFareData.FareInfant = tempFareData.FareChild;
                                        tempFareData.TotalPax = tempFareData.TotalAdult + tempFareData.TotalChild + tempFareData.TotalInfant;
                                        hasChangedClass = true;
                                    }
                                }

                                if (!isRemove)
                                {
                                    tempFareData.ListReturnFlight.Add(flight);
                                }
                            }
                        }
                    }
                }
                else
                {
                    //Không có CNN
                    if (fareData.TotalInfant > 0)
                    {
                        //Chỉ có ADT & INF
                        foreach (Flight flight in fareData.ListDepartureFlight) //Chuyến đi
                        {
                            bool isRemove = false;
                            foreach (AvailFlt availFlt in flight.ListAvailFlt)
                            {
                                if (availFlt.ClassAdult != availFlt.ClassInfant)
                                {
                                    //Nếu class ADT & INF khác nhau thì bỏ qua chặng này
                                    isRemove = true;
                                    break;
                                }
                            }

                            if (!isRemove)
                            {
                                tempFareData.ListDepartureFlight.Add(flight);
                            }
                        }

                        if (fareData.ListReturnFlight != null)
                        {
                            foreach (Flight flight in fareData.ListReturnFlight) //Chuyến về
                            {
                                bool isRemove = false;
                                foreach (AvailFlt availFlt in flight.ListAvailFlt)
                                {
                                    if (availFlt.ClassAdult != availFlt.ClassInfant)
                                    {
                                        //Nếu class ADT & INF khác nhau thì bỏ qua chặng này
                                        isRemove = true;
                                        break;
                                    }
                                }

                                if (!isRemove)
                                {
                                    tempFareData.ListReturnFlight.Add(flight);
                                }
                            }
                        }
                    }
                    else
                    {
                        //Chỉ có ADT
                        //listFareData = listFareDataInput;
                        //goto Done;
                        return listFareDataInput;
                    }
                }

                //Tính lại tổng giá
                tempFareData.TotalFare = tempFareData.TotalAdult * tempFareData.FareAdult + tempFareData.TotalChild * tempFareData.FareChild + tempFareData.TotalInfant * tempFareData.FareInfant;

                //Xác định xem lựa chọn FareData này có sự upgrade INF lên CNN hay không
                tempFareData.HasChangedClass = hasChangedClass;

                listFareData.Add(tempFareData);
            }

            //Done:
            List<FareData> listFareData1 = new List<FareData>();
            listFareData1 = (from a in listFareData
                             where a.ListDepartureFlight.Count > 0 && (a.HasReturn ? a.ListReturnFlight.Count > 0 : true)
                             select a).ToList();

            //for (int i = 0; i < listFareData1.Count; i++)
            //{
            //    listFareData1[i].FareDataId = i;
            //}

            return listFareData1;
        }

        //Booking
        public string GetBookingCode(XmlDocument doc)
        {
            string code = "";
            try
            {
                XmlNode node;

                node = doc.SelectSingleNode("//EndTransaction/EndTransactResponse/RecLoc/text()");
                if (node != null) { code = node.Value; }
            }
            catch
            {
                return "";
            }
            return code;
        }

        //Request RulesInfo
        public string GetRulesInfo(string sourceFile, List<string> ListRulesInfo)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(sourceFile);
                if (doc != null)
                {
                    foreach (string item in ListRulesInfo)
                    {
                        XmlDocumentFragment xfrag = doc.CreateDocumentFragment();
                        xfrag.InnerXml = item;
                        doc.DocumentElement.FirstChild.AppendChild(xfrag);
                    }
                }
                return doc.InnerXml;
            }
            catch
            {
                return "Non rules info";
            }
        }

        //Parse RulesData
        public List<RulesData> GetListRulesData(XmlDocument rulesInfo)
        {
            List<RulesData> list = new List<RulesData>();
            XmlNodeList nodeList = rulesInfo.SelectNodes("//FareQuoteMultiDisplay_10/FareInfo/RulesData");
            XmlNode node;
            foreach (XmlElement e in nodeList)
            {
                RulesData rule = new RulesData();
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(e.OuterXml);

                node = xml.SelectSingleNode("//UniqueKey/text()");
                if (node != null) { rule.UniqueKey = node.Value; }

                node = xml.SelectSingleNode("//RulesDataType/text()");
                if (node != null) { rule.RulesDataType = node.Value; }

                node = xml.SelectSingleNode("//RulesText/text()");
                if (node != null) { rule.RulesText = node.Value; }

                list.Add(rule);
            }

            return list;
        }

        //Request offline PNR
        public XmlDocument GetPNR(string sourceFile, string bookingCode)
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                string xml = "";
                using (StreamReader sr = new StreamReader(sourceFile))
                {
                    xml = sr.ReadToEnd();
                }
                xml = xml.Replace("PNRCODE", bookingCode);

                doc.LoadXml(xml);
                GalileoConnection conn = new GalileoConnection();
                string strResponse = conn.SubmitXml(xml, "GWS", "", "");
                if (strResponse != "")
                {
                    doc.LoadXml(strResponse);
                }
            }
            catch
            {
                return doc;
            }
            return doc;
        }

        protected string ConvertClass(string originClass)
        {
            List<string> list = new List<string>();
            list.Add("J");
            list.Add("C");
            list.Add("D");
            list.Add("I");
            list.Add("Z");
            list.Add("SkyBoss");
            list.Add("Business");

            string businessClass = list.SingleOrDefault(a => a.ToLower() == originClass.ToLower());
            if (string.IsNullOrEmpty(businessClass))
            {
                return "Economic";
            }
            else
            {
                return "Business";
            }
        }
    }
}
